<?php
/**
 * Created by PhpStorm.
 * Date: 2023/5/12
 * Time: 15:07
 */

namespace App\backend\models;


use App\common\models\BaseModel;
use Illuminate\Support\Facades\DB;

class LifeFreeModel extends BaseModel
{
    const table = 'activity_lifefree';

    const _field = ['id', 'activity_name','create_time','ticket_num','trigger_by_ticket','open_specify_num','trigger_by_time','open_specify_time','trigger_method','draw_prize_num'];

    static $_instance;

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 查询字段规则
     *
     * @return array
     */
    static public function rules()
    {
        return [];
    }

    /**
     * 添加/编辑 基础设置--字段规则
     * @return array
     */
    static public function modify_rules()
    {
        return [
            'activity_name' => 'required|string|max:20', //活动名称
            'start_time' => 'required|string', //开始时间
            'end_time' => 'required|string', //结束时间
            'activity_channels' => 'required|string', //活动渠道（1.H5，2.微信小程序）
            'activity_rules' => 'required', //抽奖规则
            'authority' => 'required|int', //抽奖权限（0：全部用户，1：部分用户）
        ];
    }

    /**
     * 奖品设置--字段规则
     * @return string[]
     */
    static public function modify_prize_rules()
    {
        return [
            'id' => 'required|integer', // 活动ID
            'prize_img' => 'required|string', //奖品图片
            'prize_name' => 'required|string', //奖品名称
            'no_prize_name' => 'required|string', //未中奖名称：谢谢参与
            'no_prize_img' => 'required|string', //未中奖图片
            'no_prize_tip' => 'required|string', //未中奖提示语
        ];
    }

    /**
     * 奖品设置--字段规则
     * @return string[]
     */
    static public function modify_draw_prize_rules()
    {
        return [
            'id' => 'required|integer', // 活动ID
            'ticket_num' => 'required|integer', //发奖数量
            'trigger_by_ticket' => 'integer', //卡券数量触发
            'open_ticket_num' => 'integer', //开奖奖券数量
            'trigger_by_time' => 'integer', //指定数时间触发
//            'open_specify_time' => 'string', //开奖指定时间
            'trigger_method' => 'required|integer', //触发方式：奖券和时间条件 且或选择（1：且，2：或）
            'draw_prize_num' => 'required|integer', //开奖数量份数
//            'system_win_chance' => 'required|string', //系统中奖概率
            'user_win_chance' => 'required|string', //用户中奖概率
            'appoint_win' => 'required|integer', //指定中奖(1:用户，2:券)
//            'win_user' => 'required|string', //指定中奖用户
//            'win_ticket' => 'required|string', //指定中奖券
        ];
    }

    /**
     * 字段验证返回错误信息
     * @return string[]
     */
    static public function message()
    {
        return [
            'id.required' => '活动ID不能为空',
            'id.integer' => '活动ID类型错误',
            'activity_name.required' => '活动名称不能为空',
            'activity_name.max' => '活动名称长度超过限制',
            'start_time.required' => '开始时间不能为空',
            'end_time.required' => '结束时间不能为空',
            'activity_channels.required' => '活动渠道不能为空',
            'activity_rules.required' => '抽奖规则不能为空',
            'authority.required' => '抽奖权限不能为空',
            'prize_img.required' => '奖品图片不能为空',
            'prize_name.required' => '奖品名称不能为空',
            'no_prize_name.required' => '未中奖名称不能为空',
            'no_prize_img.required' => '未中奖图片不能为空',
            'no_prize_tip.required' => '未中奖提示语不能为空',

            'trigger_by_ticket.integer' => '卡券数量触发类型错误',
            'ticket_num.required' => '发奖券数量不能为空',
            'ticket_num.integer' => '奖券数量类型错误',
            'trigger_by_time.integer' => '指定数时间触发类型错误',
            'trigger_method.required' => '开奖触发方式不能为空',
            'trigger_method.integer' => '开奖触发方式类型错误',
            'draw_prize_num.required' => '开奖数量不能为空',
            'draw_prize_num.integer' => '开奖数量类型错误',
            'user_win_chance.required' => '用户中奖概率不能为空',
            'appoint_win.required' => '指定中奖不能为空',
            'appoint_win.integer' => '指定中奖不类型错误',
        ];
    }

    /**
     * 查询分页的列表
     * @param $condition  查询条件
     * @param $page 分页页数
     * @param $limit 分页条数
     * @param $condition_in in 查询条件
     * @param $field
     * @return mixed
     */
    static public function getPageList($condition, $page, $limit,$condition_in = [], array $field = self::_field)
    {
        $query = DB::table(self::table)
            ->select($field)
            ->where($condition);

        if (!empty($condition_in)) {
            $query->whereIn('id', $condition_in);
        }

        $data = $query->apiPaginate($limit, ['*'], 'page', $page)->toArray();
        return $data;
    }


    /**
     * 获取数据
     * @param $condition
     * @param array $field
     * @return array
     */
    public function getList($condition, array $field = self::_field)
    {
        $condition['is_delete'] = 0;
        return DB::table(self::table)->select($field)->where($condition)->get()->toArray();
    }

    /**
     * 获取数据数量
     * @param $condition
     * @param array $field
     * @return int
     */
    public function getCount($condition, array $field = self::_field)
    {
        $condition['is_delete'] = 0;
        return DB::table(self::table)->select($field)->where($condition)->count();
    }

    /**
     * 获取数据
     * @param $condition
     * @param $field
     * @return
     */
    public function getInfo($condition, array $field = self::_field)
    {
        $condition['is_delete'] = 0;
        return DB::table(self::table)->select($field)->where($condition)->first();
    }

    /**
     * 保存数据
     * @param $data
     * @return bool
     */
    public function insertSave($data)
    {
        if (!$data) {
            return '';
        }
        $data['create_time'] = time();
        return DB::table(self::table)->insertGetId($data);
    }

    /**
     * 修改数据
     * @param $condition
     * @param $data
     * @return int|string
     */
    public function updateSave($condition, $data)
    {
        if (!$condition || !$data) {
            return '';
        }
        $data['update_time'] = time();
        return DB::table(self::table)->where($condition)->update($data);
    }

    /**
     * 删除数据
     * @param $condition
     * @return int|string
     */
    public function deleteData($condition)
    {
        if (!$condition) {
            return '';
        }
        return DB::table(self::table)->where($condition)->update(['is_delete' => 1]);

        try {
            DB::beginTransaction();
            //删除活动
            DB::table(self::table)->where($condition)->delete();
            //商城活动参与记录
            DB::table('activity_lifefree_log')->where($condition)->delete();
            DB::commit();
            return array('state' => 200, 'msg' => 'success');
        } catch (Exception $e) {
            DB::rollback();
            return array('state' => false, 'msg' => $e->getMessage());
        }
    }
}
